<!-- 单据详情 -->
<template>
	<view>
		<!-- 顶部导航 -->
		<view>
			<navitop>
				<i class="iconfont icon-jiantou-copy" slot="left-slot" @click="previous_page"></i>
				<span slot="center-slot"><h4>{{title}}</h4></span>
				<i  slot="right-slot" class="iconfont icon-youcecaidan" v-if="rightList" @click="library_click" ></i>
			</navitop> 
		</view>
		<!-- 基本信息 -->
		<view :style="'padding-top:' + nv_height">
			<view class="library_details">
				<div class="library_list"> 
					<div class="library_list_top">
						<h5>基本信息</h5>
					</div>
					<div class="library_list_nav">
						<div class="library_list_left">
							单据编号：
						</div>
						<div class="library_list_right"> 
							{{dataSorce.voucherCode}}
						</div>
					</div>
					<div class="library_list_nav">
						<div class="library_list_left">
							单据时间：
						</div>
						<div class="library_list_right"> 
							{{dataSorce.voucherDate}}
						</div>
					</div>
					<div class="library_list_nav">
						<div class="library_list_left">
							仓库：
						</div>
						<div class="library_list_right"> 
							{{dataSorce.warehouseName}}
						</div>
					</div>
					<div class="library_list_nav" v-if="type_txt_nae">
						<div class="library_list_left">
							业务类型：
						</div>
						<div class="library_list_right"> 
							{{type_display(dataSorce.businessType)}}
						</div>
					</div>
				</div>
			</view>
			
			<!-- 入库物品 -->
			<view class="library_details">
				<div class="library_list"> 
					<div class="library_list_top">
						<h5>物料列表</h5>
					</div>
					<div class="library_commodity" v-for="(item,index) in dataSorce.detailList">
						<div class="library_commodity_center">
							<div>
								物料编码：
							</div>
							<div>
								{{item.inventoryCode}}
							</div>
						</div>
						<div class="library_commodity_center">
							<div>
								物料名称：
							</div>
							<div>
								{{item.inventoryName}}
							</div>
						</div>
						<div class="library_commodity_center">
							<div>
								规格型号：
							</div>
							<div>
								{{item.inventorySpecification}}
							</div>
						</div>
						
						<div class="library_commodity_center">
							<div>
								{{type_txt}}：
							</div>
							<div>
								{{item.baseQuantity}}
							</div> 
						</div>
					</div>
				</div> 
			</view>
			<view class="library_details">
				<div class="library_list">
					<div class="library_list_name">
						<div class="library_list_name_hr">
							<div>
								制单人
							</div>
							<div>
								{{dataSorce.createUserName}}
							</div>
						</div>
						<div class="library_list_name_tow">
							<div>
								单据时间
							</div>
							<div>
								{{dataSorce.voucherDate}}
							</div>
						</div>
					</div>
				</div>
			</view>
		</view>
		
		
		
		<!-- 超出最低库存列表 -->
		<popus ref="list_number">
			<view :style="'width:' + bo_windth " >
				<view class="number_list" >
					<view class="number_list_title">
						库存预警
					</view>
					<view class="number_box">
						<view class="number_list_type" v-for="(item,index) in load_list">
							<view class="unmber_flex">
								<view class="number_list_left">
									厂库:
								</view>
								<view class="number_list_right">
									{{item.warehouseName}}
								</view>
							</view>
							<view class="unmber_flex">
								<view class="number_list_left">
									物料:
								</view>
								<view class="number_list_right">
									{{item.inventoryName}}
								</view>
							</view>
							<view class="unmber_flex">
								<view class="number_list_left">
									规格型号:
								</view>
								<view class="number_list_right">
									{{item.inventorySpecification}}
								</view>
							</view>
							<view class="unmber_flex">
								<view class="number_list_left">
									出库数量:
								</view>
								<view class="number_list_right">
									{{item.baseQuantity}}
								</view>
							</view>
							<view class="unmber_flex">
								<view class="number_list_left">
									出库前数量:
								</view>
								<view class="number_list_right">
									{{item.beforeInventoryQuantity}}
								</view>
							</view>
							<view class="unmber_flex">
								<view class="number_list_left">
									出库后数量:
								</view>
								<view class="number_list_right">
									{{item.afterInventoryQuantity}}
								</view>
							</view>
							<view class="unmber_flex">
								<view class="number_list_left">
									最低库存:
								</view>
								<view class="number_list_right" style="color: red;">
									{{item.minStorage}}
								</view>
							</view>
						</view>
					</view>
					<view class="number_list_bottom">
						<view class="number_list_txt" style="background-color: #555555;" @click="numberout">
							取消入库
						</view>
						<view class="number_list_txt" @click="numbercontinue">
							继续入库
						</view>
					</view>
				</view>
			</view>
		</popus>
		
		
		<!-- 气泡弹窗 -->
		 <chunLei-popups v-model="value" :popData="data" @tapPopup="tapPopup" :x="345" :y="95" placement="top-end">
		 </chunLei-popups>
	</view>
</template>

<script>

import popus from '@/uni_modules/uni-popup/components/uni-popup/uni-popup'
import navitop from '@/components/common/navi-top'	
import chunLeiPopups from "@/components/chunLei-popups/chunLei-popups";
import {DynamicById,recorddelete,purchasesubmit,examine,inconfirm,deliverysubmit,liveryexamine,lop_submit,proexamine,pickRedelete,delidelete} from "@/common/api/job/bill.js"
export default {
	components:{
		navitop,
		popus,
		chunLeiPopups
	},
	data(){
		return {
			title: "详情列表",
			value:false,
			data: [
				// {title:'修改',code:'libarary_edit',icon:'icon-xiugai'},
				// {title:'提交',code:'libarary_up',icon:'icon-tijiao'},
				// {title:'撤回',code:'libarary_noup',icon:'icon-tuihui'},
				// {title:'审核',code:'libarary_examine',icon:'icon-shenhe'},
				// {title:'弃审',code:'libarary_noexamine',icon:'icon-icon_function_shenhetuihui'},
				// // {title:'入库',code:'libarary_housing',icon:'icon-ruku'},
				// {title:'删除',code:'libarary_del',icon:'icon-shanchu'},
			],	// 气泡弹窗数据
			nv_height: '',
			url_dataSorce: '',
			dataSorce: '',
			display_type: true,
			collar_data:['RRS','MaterialDispatchVoucher','DelegateRRS','DelegateDispatchVoucher'],	//领料处理
			rightList: true,
			type_txt: '',
			bo_windth: '',
			type_list: [
				{"key":102,"label":"盘亏出库"},
				{"key":107,"label":"调拨出库"},
				{"key":110,"label":"其他出库"},
				{"key":101,"label":"盘盈入库"},
				{"key":106,"label":"调拨入库"},
				{"key":109,"label":"其他入库"}
			],
			type_txt_nae: false,//	其他出库，其他入库显示类型
			
			isCheckInventoryInfo: true,	// 是否检查最低库存
			
			load_list: [],	// 最低库存列表
			
			numberType: false,	// 防止重复点击提交数据错乱
			
			bottomData: '', // 点击按钮
			
		}
	},
	onLoad(option) {
		
		this.url_dataSorce = option;
		if(option.c_code == 0){
			this.display_type = false;
		}

		
		if(option.code == 'PurchaseReceiveVoucher' && option.sort == '0'){	// 采购入库
			this.warehouse_pu(1);
		}else if(option.code == 'PurchaseReceiveVoucher' && option.sort == '99'){ // 采购退货
		
			this.warehouse_pu(2);
			
		}else if(option.code == 'SaleDispatchVoucher' && option.sort == '0'){	// 销售出库单
			
			this.warehouse_pu(2);
			
		}else if(option.code == 'SaleDispatchVoucher' && option.sort == "98"){	// 销售退货单
			
			this.warehouse_pu(3);
			
		}else if(option.code == 'ProductReceiveVoucher'){	//产品入库单
			if(option.c_code == 2){
				this.warehouse_pu(2);
			}else{
				this.warehouse_pu(1);
			}
			
		}else 
		// if(option.code == 'MaterialDispatchVoucher'){	// 材料出库单
			
		// 	this.warehouse_pu(2);
			
		// }else
		 if(option.code == 'DelegateReceiveVoucher'){	// 委外入库单
			this.warehouse_pu(1);
			
		}else 
		// if(option.code == 'DelegateDispatchVoucher'){	// 委外出库单
		
		// 	this.warehouse_pu(2);
			
		// }else 
		if(option.code == 'OtherReceiveVoucher'){	//	其他入库单
			this.type_txt_nae = true;
			this.warehouse_pu(1);
			
		}else if(option.code == 'OtherDispatchVoucher'){	//	其他入库单
			this.type_txt_nae = true;
			this.warehouse_pu(2);
			
		}else if(option.code == 'MaterialDispatchVoucher' && option.c_code == 1){	//生产退料
		
			this.warehouse_pu(1);
			
		}else if(option.code == 'MaterialDispatchVoucher' && option.c_code == 2){	// 生产出库
		
			this.warehouse_pu(2);
			
		}else if(option.code == 'DelegateDispatchVoucher' && option.c_code == 1){	//委外退料
		
			this.warehouse_pu(1);
			
		}else if(option.code == 'DelegateDispatchVoucher' && option.c_code == 2){	// 委外领料
		
			this.warehouse_pu(2);
			
		}
	},
	mounted() {
		 this.nv_height = uni.getSystemInfoSync().statusBarHeight + 65 + 'px';
		 
		this.bo_windth = uni.getSystemInfoSync().windowWidth + 'px';
		 
		 this.From_list();
	},
	methods: {
		// 返回上一页
		previous_page(){
			uni.navigateBack()
		},
		
		// 区分业务类型 其他出入库
		type_display(val){
			let dataname = '';
			this.type_list.forEach(item => {
				if(item.key == val){
					dataname = item.label;
				}
			})
			return dataname;
		},
		
		// 出入库气泡弹窗数组
		warehouse_pu(val){
			if(val == 1){
				this.data.push({title:'入库',code:'libarary_housing',icon:'icon-ruku'})
			}
			if(val == 2){
				this.data.push({title:'出库',code:'libarary_housi',icon:'icon-chukudan'})
			}
			if(val == 3){
				this.data.push({title:'退货入库',code:'libarary_housing',icon:'icon-chukudan'})
			}
		},
		
		// 数据回显
		From_list(){
			DynamicById({currentId:this.url_dataSorce.id,source:this.url_dataSorce.code,isMyWarehouse:true,someBusinessType:0}).then((row) => {
				if(row.code == 200){
					
					// -1 , 未提交  , 0 ,已提交待审核   1 , 已审核   2, 不显示任何按钮
					
					if(row.data.voucherState == -1){
						this.data.push({title:'提交',code:'libarary_up',icon:'icon-tijiao'})
					}
					if(row.data.voucherState == 0){
						this.data.push({title:'撤回',code:'libarary_noup',icon:'icon-tuihui'})
						this.data.push({title:'审核',code:'libarary_examine',icon:'icon-shenhe'})
					}
					if(row.data.voucherState == 1){
						this.data.push({title:'弃审',code:'libarary_noexamine',icon:'icon-icon_function_shenhetuihui'})
					}
					if(row.data.voucherState == 2){
						this.rightList = false;
					}
					
					this.dataSorce = row.data;
					if(row.data.receiveDeliverType == 1){
						this.type_txt = '实收数量'
					}else if(row.data.receiveDeliverType == 2){
						this.type_txt = '实发数量'
					}else{
						this.type_txt = "数量"
					}
				}
			})
		},
		
		
		// 通知单统一删除
		delete_lou(){
			uni.showModal({
				title: '提示',
				content: '确认要删除吗?',
				success: (res) => {
					if (res.confirm) {
						delidelete(this.url_dataSorce.id).then((item) => {
							if(item.code == 200){
								uni.showToast({
									title: '删除成功！',	
									icon: 'none'
								});
								setTimeout(() => {
									uni.navigateTo({
										url: "/pages/common/index/purchase/common?title=" + this.url_dataSorce.title + "&sort=" + this.url_dataSorce.sort + "&key=" + this.url_dataSorce.key + "&c_code=" + this.url_dataSorce.c_code +"&code=" + this.url_dataSorce.code  + "&search_code=" + this.url_dataSorce.search_code
									})
								}, 1500);
							}else {
								uni.showToast({
									title: item.message,
									icon: 'none'
								})
							}
						})
					}else if (res.cancel) {
						//用户点击取消
					}
				}
			})
		},
		
		// 申请单统一删除
		delete_l_sp(){
			uni.showModal({
				title: '提示',
				content: '确认要删除吗?',
				success: (res) => {
					if (res.confirm) {
						pickRedelete(this.url_dataSorce.id).then((item) => {
							if(item.code == 200){
								uni.showToast({
									title: '删除成功！',	
									icon: 'none'
								}); 
								setTimeout(() => {
									uni.navigateTo({
										url: "/pages/common/index/purchase/common?title=" + this.url_dataSorce.title + "&sort=" + this.url_dataSorce.sort + "&key=" + this.url_dataSorce.key + "&c_code=" + this.url_dataSorce.c_code +"&code=" + this.url_dataSorce.code  + "&search_code=" + this.url_dataSorce.search_code
									})
								}, 1500);
							}else {
								uni.showToast({
									title: item.message,
									icon: 'none'
								})
							}
						})
					}else if (res.cancel) {
						//用户点击取消
					}
				}
			})
		},
		
		// 气泡弹窗操作
		tapPopup(val){
			let collar_type = '';
			// 删除
			if(val.code == 'libarary_del'){
				// 发货通知单删除
				if(this.url_dataSorce.code == 'SaleDelivery' && this.url_dataSorce.c_code == 0){
					this.delete_lou();
				}else if(this.url_dataSorce.code == 'SaleDelivery' && this.url_dataSorce.c_code == 1){
					// 退货通知单
					this.delete_lou();
				}else  if(this.url_dataSorce.code == 'RRS' || this.url_dataSorce.code == 'DelegateRRS'){
					uni.showModal({
						title: '提示',
						content: '确认要删除吗?',
						success: (res) => {
							if (res.confirm) {
								pickRedelete(this.url_dataSorce.id).then((item) => {
									if(item.code == 200){
										uni.showToast({
											title: '删除成功！',	
											icon: 'none'
										});
										setTimeout(() => {
											uni.navigateTo({
												url: "/pages/common/index/purchase/common?title=" + this.url_dataSorce.title + "&sort=" + this.url_dataSorce.sort + "&key=" + this.url_dataSorce.key + "&c_code=" + this.url_dataSorce.c_code +"&code=" + this.url_dataSorce.code  + "&search_code=" + this.url_dataSorce.search_code
											})
										}, 1500);
									}else {
										uni.showToast({
											title: item.message,
											icon: 'none'
										})
									}
								})
							}else if (res.cancel) {
								//用户点击取消
							}
						}
					})
					this.delete_l_sp();
				}else {
					uni.showModal({
						title: '提示',
						content: '确认要删除吗?',
						success: (res) => {
							if (res.confirm) {
								recorddelete(this.url_dataSorce.id).then((item) => {
									if(item.code == 200){
										uni.showToast({
											title: '删除成功！',	
											icon: 'none'
										});
										setTimeout(() => {
											uni.navigateTo({
												url: "/pages/common/index/purchase/common?title=" + this.url_dataSorce.title + "&sort=" + this.url_dataSorce.sort + "&key=" + this.url_dataSorce.key + "&c_code=" + this.url_dataSorce.c_code +"&code=" + this.url_dataSorce.code  + "&search_code=" + this.url_dataSorce.search_code
											})
										}, 1500);
									}else {
										uni.showToast({
											title: item.message,
											icon: 'none'
										})
									}
								})
							}else if (res.cancel) {
								//用户点击取消
							}
						}
					})
				}
				
			}
			
			//修改
			if(val.code == 'libarary_edit'){ 	
				this.collar_data.forEach(item => {
					if(this.url_dataSorce.code == item){
						collar_type = 1
					}
				})
				if(collar_type){
					uni.navigateTo({
						url: "/pages/common/index/purchase/science/science-edit?title=" + this.url_dataSorce.title + "&sort=" + this.url_dataSorce.sort + "&key=" + this.url_dataSorce.key + "&c_code=" + this.url_dataSorce.c_code +"&code=" + this.url_dataSorce.code + '&id=' + this.url_dataSorce.id
					})
				}else{
					uni.navigateTo({
						url: "/components/common/bill/bill-details_edit?title=" + this.url_dataSorce.title + "&sort=" + this.url_dataSorce.sort + "&key=" + this.url_dataSorce.key + "&c_code=" + this.url_dataSorce.c_code +"&code=" + this.url_dataSorce.code + '&id=' + this.url_dataSorce.id
					})
				}
			}
			
			// 提交
			if(val.code == 'libarary_up'){
				this.data_up(0)
			}
			
			// 撤销
			if(val.code == 'libarary_noup') {
				this.data_up(-1)
			}
			
			// 审核
			if(val.code == 'libarary_examine') {
				this.examine_type(1)
			}
			
			// 弃审
			if(val.code == 'libarary_noexamine') {
				this.examine_type(0)
			}
			
			// 入库
			if(val.code == 'libarary_housing') {
				this.housing_type();
				this.bottomData = 'libarary_housing';
			}
			
			// 出库
			if(val.code == 'libarary_housi'){
				this.housing_type_li();
				this.bottomData = 'libarary_housi';
			}
			
		},
		
		// 出库
		housing_type_li(){
			inconfirm({id:this.url_dataSorce.id,isCheckInventoryInfo: this.isCheckInventoryInfo}).then(rst => {
				if(rst.code == 200){
					uni.showToast({
						title: '出库成功',
						icon: 'none'
					})
				}else if(rst.code == 5002){
					this.isCheckInventoryInfo = false;
					this.load_list = JSON.parse(rows.message);
					this.$refs.list_number.open('center');
					console.log('5002-------------')
				}else{
					uni.showToast({
						title: rst.message,
						icon: 'none'
					})
				}
				this.dow_out();
			})
		},
		
		// 入库
		housing_type(){
			inconfirm({id:this.url_dataSorce.id,isCheckInventoryInfo: this.isCheckInventoryInfo}).then(rst => {
				if(rst.code == 200){
					uni.showToast({
						title: '入库成功',
						icon: 'none'
					})
				}else if(rst.code == 5002){
					this.isCheckInventoryInfo = false;
					this.load_list = JSON.parse(rows.message);
					this.$refs.list_number.open('center');
					console.log('5002-------------')
				}else{
					uni.showToast({
						title: rst.message,
						icon: 'none'
					})
				}
				this.dow_out();
			})
		},
		
		// 审核 
		examine_type(val){
			if(this.url_dataSorce.code == 'SaleDelivery'){	// 发货通知单
				liveryexamine({id: this.url_dataSorce.id,status: val}).then(item => {
					if(item.code == 200){
						if(val == 1){
							uni.showToast({
								title: '审核成功！',
								icon: 'none'
							})
						}else if(val == 0){
							uni.showToast({
								title: '弃审成功！',
								icon: 'none'
							})
						}
						this.dow_out();
					}
				})
			}else if(this.url_dataSorce.code == 'RRS' || this.url_dataSorce.code == 'DelegateRRS'){
				proexamine({id: this.url_dataSorce.id,status: val}).then(item => {
					if(item.code == 200){
						if(val == 1){
							uni.showToast({
								title: '审核成功！',
								icon: 'none'
							})
						}else if(val == 0){
							uni.showToast({
								title: '弃审成功！',
								icon: 'none'
							})
						}
						this.dow_out();
					}
				})
			}else {
				examine({id: this.url_dataSorce.id,status: val}).then(item => {
					if(item.code == 200){
						if(val == 1){
							uni.showToast({
								title: '审核成功！',
								icon: 'none'
							})
						}else if(val == 0){
							uni.showToast({
								title: '弃审成功！',
								icon: 'none'
							})
						}
						this.dow_out();
					}
				})
			}
				
		},
		
		// 提交
		data_up(val){
			if(this.url_dataSorce.code == 'SaleDelivery'){
				deliverysubmit({id: this.url_dataSorce.id,status: val}).then(item => {
					if(item.code == 200){
						if(val == 0){
							uni.showToast({
								title: '提交成功！',
								icon: 'none'
							})
						}else if(val == -1){
							uni.showToast({
								title: '撤回成功！',
								icon: 'none'
							})
						}
						this.dow_out();	
					}
				})
			}else if(this.url_dataSorce.code == 'RRS' || this.url_dataSorce.code == 'DelegateRRS'){
				lop_submit({id: this.url_dataSorce.id,status: val}).then(item => {
					if(item.code == 200){
						if(val == 0){
							uni.showToast({
								title: '提交成功！',
								icon: 'none'
							})
						}else if(val == -1){
							uni.showToast({
								title: '撤回成功！',
								icon: 'none'
							})
						}
						this.dow_out();	
					}
				})
			}else {
				purchasesubmit({id: this.url_dataSorce.id,status: val}).then(item => {
					if(item.code == 200){
						if(val == 0){
							uni.showToast({
								title: '提交成功！',
								icon: 'none'
							})
						}else if(val == -1){
							uni.showToast({
								title: '撤回成功！',
								icon: 'none'
							})
						}
						this.dow_out();	
					}
				})
			}
		},
		
		// 返回上一页 
		dow_out(){
			setTimeout(() => {
				uni.navigateTo({
					url: '/pages/common/index/purchase/common?title=' 
					+ this.url_dataSorce.title + 
					'&sort=' + this.url_dataSorce.sort +
					'&code=' + this.url_dataSorce.code +
					'&key=' + this.url_dataSorce.key +
					'&c_code=' + this.url_dataSorce.c_code +
					'&search_code=' + this.url_dataSorce.search_code
				})
			},1500)
		},
		
		// 显示隐藏气泡弹窗
		library_click(){
			this.value = !this.value;
		},
		
		// 取消
		numberout(){
			 this.isCheckInventoryInfo = true;
			this.$refs.list_number.close();
		},
		
		// 确定入库
		numbercontinue(){
			// 入库
			if(this.bottomData == 'libarary_housing') {
				this.housing_type()
			}
			
			// 出库
			if(this.bottomData == 'libarary_housi'){
				this.housing_type_li()
			}
		}
	}
}
	
</script>

<style scoped lang="scss">
	

	.number_list {
		width: 95%;
		margin: auto;
		background-color: #fff;
		border-radius: 4px;
		height: 500px;
		
		.number_list_title {
			text-align: center;
			padding: 5px 0;
		}
		.number_box {
			height: 410px;
			overflow-y: scroll;
		}
		
		.number_list_type {
			padding: 5px 0;
			border-bottom: 1px solid #cacaca;
			.unmber_flex {
				display: flex;
			}
			.number_list_left {
				width: 100px;
				text-align: right;
				padding: 0 5px 0 0;
				overflow: hidden;
				white-space: nowrap;
			}
			.number_list_right {
				overflow: hidden;
			}
		}
		
		.number_list_bottom {
			display: flex;
			justify-content: space-around;
		}
		.number_list_txt {
			background-color: #007AFF;
			color: #fff;
			text-align: center;
			padding: 5px 10px;
			border-radius: 4px;
			box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.2);
		}
	}	
	
.library_details {
	width: 100%;
	margin-bottom: 10px;
	div {
		overflow: hidden;
	}
}

.library_list {
	width: 90%;
	margin: auto;
	background-color: #fff;
	border-radius: 4px;
	padding: 0 0 10px 15px;
	box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
	
}

.library_list_top {
	height: 80rpx;
	line-height: 80rpx;
	width: 95%;
}

.library_list_nav {
	padding: 0 0 0 15px;
	display: flex;
	overflow: hidden;
	width: 90%;
	div{
		height: 40rpx;
		line-height: 40rpx;
	}
	
	.library_list_left {
		width: 30%;
	}
}


.library_commodity {
	margin-top: 5px;
	padding: 10rpx 0 20rpx 0;
	border-bottom: 1px solid #cacaca;
}

.library_commodity_center {
	padding: 0 0 0 20rpx;
	display: flex;
}


.library_list_name {
	div{
		height: 80rpx;
		line-height: 80rpx;
	}
	
	.library_list_name_hr {
		width: 95%;
		display: flex;
		justify-content: space-between;
	}
}

.library_list_name_tow {
	display: flex;
	justify-content: space-between;
	width: 95%;
}
	
</style>
